function DlmanageUtils() {
};

DlmanageUtils.order = 'LEFT OUTER JOIN ns__ez_relation_customer_myorder AS co ON c.e_oid = co.oid_a LEFT OUTER JOIN ns_myorder AS o ON o.e_oid = co.oid_b';
DlmanageUtils.task = 'LEFT OUTER JOIN ns__ez_relation_myorder_ordertask AS ot ON o.e_oid = ot.oid_a LEFT OUTER JOIN ns_ordertask AS t ON t.e_oid = ot.oid_b';
DlmanageUtils.user = 'LEFT OUTER JOIN ns__ez_relation_ordertask_ordertaskuser AS tu ON t.e_oid = tu.oid_a LEFT OUTER JOIN ns_ordertaskuser AS u ON u.e_oid = tu.oid_b';
DlmanageUtils.produce = 'LEFT OUTER JOIN ns__ez_relation_myorder_orderproduce AS op ON o.e_oid = op.oid_a LEFT OUTER JOIN ns_orderproduce AS p ON p.e_oid = op.oid_b';
DlmanageUtils.assignee = 'LEFT OUTER JOIN ns__ez_relation_assignee_orderproduce AS ap ON p.e_oid = ap.oid_a LEFT OUTER JOIN ns_assignee AS a ON a.e_oid = ap.oid_b';

DlmanageUtils.status1 = 'LEFT OUTER JOIN ns__ez_relation_myorder_status AS os ON o.e_oid = os.oid_a LEFT OUTER JOIN ns_status AS s ON s.e_oid = os.oid_b';
DlmanageUtils.status2 = 'LEFT OUTER JOIN ns__ez_relation_ordertaskuser_status AS us ON u.e_oid = us.oid_a LEFT OUTER JOIN ns_status AS s ON s.e_oid = us.oid_b';
DlmanageUtils.status3 = 'LEFT OUTER JOIN ns__ez_relation_orderproduce_status AS ps ON p.e_oid = ps.oid_a LEFT OUTER JOIN ns_status AS s ON s.e_oid = ps.oid_b';

DlmanageUtils.attributes = function(sample) { 
  if (sample == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}') {
    return [ { name: '形状', options: [ { name: '横' }, { name: '竖' }, { name: '方' } ] },
             { name: '膜', options: [ { name: '裱膜' }, { name: '淋膜' } ] },
             { name: '封面', options: [ { name: '银沙' }, { name: '铭牌' } ] },
             { name: 'Pages', options: [ { name: '10' }, { name: '11' }, { name: '12' }, { name: '13' }, { name: '14' }, { name: '15' }, { name: '16' }, { name: '17' }, { name: '18' }, { name: '19' }, { name: '20' } ] }
           ];
  } else if (sample == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}') {
    return [ { name: '形状', options: [ { name: '横' }, { name: '竖' }, { name: '方' } ] },
             { name: '膜', options: [ { name: '亮膜' }, { name: '亚膜' } ] },
             { name: 'Pages', options: [ { name: '10' }, { name: '11' }, { name: '12' }, { name: '13' }, { name: '14' }, { name: '15' }, { name: '16' }, { name: '17' }, { name: '18' }, { name: '19' }, { name: '20' } ] }
           ];
  }
  return [];
};

DlmanageUtils.sample = function(product, samples) {
  for (var i = 0, il = samples.length; i < il; i++) {
    sample = samples[i];
    if (sample.id == product.sampleId) {
      return sample;
    }
  }
  return null;
};

DlmanageUtils.design = function(albums) {
  var page = 0, shape = '', description = ''; 
  for (var i = 0, il = albums.length; i < il; i++) {
    var album = albums[i];
    description += album.product.name + ((i < (il - 1)) ? ', ' : '');
    for (var j = 0, jl = album.product.selects.length; j < jl; j++) {
      var select = album.product.selects[j];
      if (select.sampleAttributeName == 'Pages') {
        page = Math.max(page, parseInt(select.name, 10));
      } else if (select.sampleAttributeName == '形状') {
        shape = select.name;
      }
    }
  }
  return { description: description, page: page, shape: shape };
};

DlmanageUtils.earliest = function(order) { 
  if (order.tasks.length == 0) {
    return null;
  }
  
  var earliest = order.tasks[0].date;
  for (var i = 0, il = order.tasks.length; i < il; i++) {
    var date = order.tasks[i].date;
    
    if (SimpleDate.toStamp(date) < SimpleDate.toStamp(earliest)) {
      earliest = date;
    }
  }
  return earliest;
};

DlmanageUtils.latest = function(order) { 
  if (order.tasks.length == 0) {
    return null;
  }
  
  var earliest = order.tasks[0].date;
  for (var i = 0, il = order.tasks.length; i < il; i++) {
    var date = order.tasks[i].date;
    
    if (SimpleDate.toStamp(date) > SimpleDate.toStamp(earliest)) {
      earliest = date;
    }
  }
  return earliest;
};

DlmanageUtils.keyword = function(model, state, transition, item) {
  return model.title + '#' + state.title + '#' + transition.title + '#' + item.trackId;
};

DlmanageUtils.content = function(keyword) {
  var contents = keyword.split('#');
  return contents[2] + ' [' + contents[1] + ']';
};

DlmanageUtils.record = function(order, now, registrar, keyword) { 
  var event = OrderEvent.instance();
  event.keyword = keyword;
  event.date = now;
  event.registrar = registrar.account;
  event.content = this.content(keyword);
  order.events[order.events.length] = event;
};

DlmanageUtils.operate = function(status, target, order, now, registrar, keyword, callbackFunc, host) {
  this.record(order, now, registrar, keyword);
  status.state = target.id;
  status.date = now;
  new RequestUtils()._write('myorder', [order], [], function() { callbackFunc(); }, { php: ((host) ? host + '/jsonwrite.php' : null) });
};

DlmanageUtils.trace = function(parent, status) {
  for (key in parent) {
    if (key == 'music' || 
        key == 'precon' ||
        key == 'edit' ||
        key == 'photo' || 
        key == 'video' || 
        key == 'tasks' || 
        key == 'photographers' || 
        key == 'videographers' || 
        key == 'makeups' || 
        key == 'produces' || 
        key == 'status' || 
        !isNaN(key)
       ) {
      var child = parent[key];
      if (child.trackId == status.trackId) {
        return parent;
      }
      var trace = this.trace(child, status);
      if (trace) {
        return trace;
      }
    }
  }
  return null;
};

DlmanageUtils.pcompleted = function(order, photographer, status) {
  var result = true;
  for (var i = 0, il = order.tasks.length; i < il; i++) {
    var task = order.tasks[i];
    for (var j = 0, jl = task.photographers.length; j < jl; j++) {
      var p = task.photographers[j];
      if ((!photographer || p.id != photographer.id) && p.status.state != status) {
        result = false;
      }
    }
  }
  return result;
};

DlmanageUtils.vcompleted = function(order, videographer, status) {
  var result = true;
  for (var i = 0, il = order.tasks.length; i < il; i++) {
    var task = order.tasks[i];
    for (var j = 0, jl = task.videographers.length; j < jl; j++) {
      var v = task.videographers[j];
      if ((!videographer || v.id != videographer.id) && v.status.state != status) {
        result = false;
      }
    }
  }
  return result;
};

DlmanageUtils.pleft = function(customer, status) {
  var result = false;
  for (var i = 0, il = customer.orders.length; i < il; i++) {
    var order = customer.orders[i];
    for (var j = 0, jl = order.tasks.length; j < jl; j++) {
      var task = order.tasks[j];
      for (var k = 0, kl = task.photographers.length; k < kl; k++) {
        var photographer = task.photographers[k];
        if (photographer.status.state == status) {
          result = true;
        }
      }
    }
  }
  return result;
};

DlmanageUtils.vleft = function(customer, status) {
  var result = false;
  for (var i = 0, il = customer.orders.length; i < il; i++) {
    var order = customer.orders[i];
    for (var j = 0, jl = order.tasks.length; j < jl; j++) {
      var task = order.tasks[j];
      for (var k = 0, kl = task.videographers.length; k < kl; k++) {
        var videographer = task.videographers[k];
        if (videographer.status.state == status) {
          result = true;
        }
      }
    }
  }
  return result;
};

DlmanageUtils.mleft = function(customer, status) {
  var result = false;
  for (var i = 0, il = customer.orders.length; i < il; i++) {
    var order = customer.orders[i];
    for (var j = 0, jl = order.tasks.length; j < jl; j++) {
      var task = order.tasks[j];
      for (var k = 0, kl = task.makeups.length; k < kl; k++) {
        var makeup = task.makeups[k];
        if (makeup.status.state == status) {
          result = true;
        }
      }
    }
  }
  return result;
};

DlmanageUtils.prodleft = function(customer, status, model, registrar) {
  var result = false;
  for (var i = 0, il = customer.orders.length; i < il; i++) {
    var order = customer.orders[i]
    for (var j = 0, jl = order.produces.length; j < jl; j++) {
      var produce = order.produces[j];
      if (model && model != produce.product.model) {
        continue;
      }
      if (registrar.type == '{{$smarty.const.USER_TYPE_DESIGNER|escape:javascript}}' && produce.assignee.userId != registrar.id) {
        continue;
      }
      if (produce.status.state == status) {
        result = true;
      }
    }
  }
  return result;
};

DlmanageUtils.edit = function(order, now) {
  var product = Product.instance();
  product.name = 'Video Editing';
  product.model = '{{$smarty.const.MODEL_NAME_PRODUCT_VIDEO|escape:javascript}}';
  product.designId = MiscUtils.unique();
  
  var produce = OrderProduce.instance();
  produce.product = product;
  produce.quantity = 1;
  produce.status.state = 1;
  produce.status.date = now;
  produce.status.model = '{{$smarty.const.MODEL_NAME_PRODUCT_VIDEO|escape:javascript}}';
  
  order.produces[order.produces.length] = produce;
};

DlmanageUtils.produces = function(order, now) {
  var produce = null;
  for (var i = 0, il = order.purchases.length; i < il; i++) {
    var purchase = order.purchases[i];
    if (purchase.product.model == '') {
      continue;
    }
    if (purchase.product.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || purchase.product.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}') {
      for (var j = 0, jl = purchase.quantity; j < jl; j++) {
        produce = MiscUtils.clone(purchase);
        produce.assignee = Assignee.instance();
        produce.events = [];
        produce.status = Status.instance();
        produce.status.state = 1;
        produce.status.date = now;
        produce.status.model = produce.product.model;
        delete produce.id;
        order.produces[order.produces.length] = produce;
      }
    } else {
      produce = MiscUtils.clone(purchase);
      produce.assignee = Assignee.instance();
      produce.events = [];
      produce.status = Status.instance();
      produce.status.state = 1;
      produce.status.date = now;
      produce.status.model = produce.product.model;
      delete produce.id;
      order.produces[order.produces.length] = produce;
    }
  }
};

DlmanageUtils.status = function(produce, model) {
  for (var i = 0, il = model.states.length; i < il; i++) {
    var state = model.states[i];
    if (state.id == produce.status.state) {
      return state;
    }
  }
  return null;
};

DlmanageUtils.delete = function(array, remove) {
  for (var i = 0, il = array.length; i < il; i++) {
    var elem = array[i];
    if (elem.id == remove.id) {
      array.splice(i, 1);
      break;
    }
  }
  return array;
};
